package com.zyx.strategy.stg03;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * <p>策略门面
 *
 * @author Yaxi.Zhang
 * @since 2022/10/10 00:42
 */
@Service
public class ThirdContext {

    @Autowired
    private List<ThirdHandler> handlerList;

    public void handle(String type) {
        ThirdHandler handler = Optional.of(handlerList).orElse(Collections.emptyList())
                .stream().filter(h -> h.accept(type))
                .findFirst().orElse(null);
        if (handler == null) {
            throw new RuntimeException("can not find handler");
        }
        handler.handler();
    }

}
